package com.schedule.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.schedule.dao.PlaceDao;
import com.schedule.model.Place;

@Controller
@RequestMapping("/place")
public class PlaceController {
	@Autowired
	private PlaceDao placeDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(){
		ModelAndView mav=new ModelAndView("place/PlaceList");
		List<Place> places=placeDao.findAll();
		System.out.println(places.size());
		mav.addObject("places",places);
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(
			@RequestParam int placeId
			){
			placeDao.deletePlace(placeId);
		ModelAndView mav=new ModelAndView("redirect:/place");
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(
			@RequestParam int placeId
			){
		Place place=placeDao.findById(placeId);
		ModelAndView mav=new ModelAndView("place/updatePlace");
		mav.addObject(place);
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(
			@RequestParam String placeId,
			@RequestParam String place
			){
		Place place2=new Place();
		place2.setPlaceId(Integer.parseInt(placeId));
		place2.setPlace(place);
		placeDao.updatePlace(place2);
		ModelAndView mav=new ModelAndView("redirect:/place");
		return mav;
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("place/addPlace");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam int placeId,
			@RequestParam String place
			){
		Place place2=new Place();
		place2.setPlaceId(placeId);
		place2.setPlace(place);
		placeDao.addPlace(place2);
		ModelAndView mav=new ModelAndView("redirect:/place");
		return mav;
	}
}
